//
//  SHD_ChipDataTableCell.m
//  B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_ChipDataTableCell.h"

@implementation SHD_ChipDataTableCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       
        _userImageView = [[UIImageView alloc]init];
        _userNameLabel = [[UILabel alloc]init];
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.textColor = [UIColor grayColor];
        _midImageView = [[UIImageView alloc]init];
        _contectLabel = [[myContectLabel alloc]init];
        _likeLabel = [[UILabel alloc]init];
        _likeLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_userImageView];
        [self.contentView addSubview:_userNameLabel];
        [self.contentView addSubview:_timeLabel];
        [self.contentView addSubview:_midImageView];
        [self.contentView addSubview:_contectLabel];
        [self.contentView addSubview:_likeLabel];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _userImageView.frame = CGRectMake(15 * FitWidth, 15 * FitHeight, 40 * FitWidth, 40 * FitHeight);
    _userImageView.layer.masksToBounds = YES;
    _userImageView.layer.cornerRadius = 20 * FitHeight;
    _userNameLabel.frame = CGRectMake(_userImageView.frame.size.width + _userImageView.frame.origin.x + 10 * FitWidth, 30 *FitHeight, 100 * FitWidth, 10 * FitHeight);
    _timeLabel.frame = CGRectMake(325 * FitWidth, _userNameLabel.frame.origin.y, 40 * FitWidth, 10 *FitHeight);
    NSArray *whArray = [[_chipModel coverimg_wh] componentsSeparatedByString:@"*"];
    CGFloat width = 345 * FitWidth;
    CGFloat hight = [whArray[1] floatValue] * 380 * FitHeight / [whArray[0] floatValue];
    
    _midImageView.frame = CGRectMake(_userImageView.frame.origin.x, _userImageView.frame.size.height + _userImageView.frame.origin.y + 20 * FitHeight, width, hight);
    _midImageView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(savePhoto:)];
    longPress.minimumPressDuration = 1;
    [_midImageView addGestureRecognizer:longPress];

    _contectLabel.numberOfLines = 0;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18 * FitWidth], NSFontAttributeName, nil];
    CGRect rect = [_chipModel.content boundingRectWithSize:CGSizeMake(345 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    _contectLabel.frame = CGRectMake(_midImageView.frame.origin.x, _midImageView.frame.origin.y + _midImageView.frame.size.height + 10 * FitHeight, width, rect.size.height + 2*FitHeight);
    _likeLabel.frame = CGRectMake(160 * FitWidth, _contectLabel.frame.size.height + _contectLabel.frame.origin.y + 10 * FitHeight, 200 * FitWidth, 10 * FitHeight);
}
#pragma mark 图片保存

-(void)savePhoto:(UILongPressGestureRecognizer *)gesture
{
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:@"提示"
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:@"保存到相册"
                                      otherButtonTitles:nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet showInView:self.contentView];
        
    }
}
#pragma mark 保存相片方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIImageWriteToSavedPhotosAlbum(_midImageView.image, self, NULL, NULL);
        
    }
}

-(void)setChipModel:(SHD_ChipMainModel *)chipModel
{
    _chipModel = chipModel;
    _midImageView.contentMode = UIViewContentModeScaleAspectFill;
    _midImageView.clipsToBounds = YES;
    UILabel *label = [[UILabel alloc]initWithFrame:_midImageView.frame];
    label.backgroundColor = [UIColor redColor];

    
//    __block UIProgressView *pv;
//    __weak UIImageView *imave = _midImageView;
//    pv = [[UIProgressView alloc]init];
//    [imave sd_setImageWithURL:[NSURL URLWithString:chipModel.coverimg] placeholderImage:nil options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//        pv.frame = CGRectMake(0, _midImageView.frame.size.height / 2, _midImageView.frame.size.width, 20);
//        [imave addSubview:pv];
//        pv.progressTintColor = [UIColor redColor];
//        CGFloat rece = receivedSize;
//        [pv setProgress:rece / expectedSize  animated:YES];
//        
//    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        [pv removeFromSuperview];
//        pv = nil;
//    }];
    
    [_midImageView sd_setImageWithURL:[NSURL URLWithString:chipModel.coverimg] placeholderImage:[UIImage sd_animatedGIFNamed:@"SHD_Scroll"]];
    [_userImageView sd_setImageWithURL:[NSURL URLWithString:[chipModel.userinfo objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"SHD_Cov"]];
    _timeLabel.text = chipModel.addtime_f;
    _timeLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];
    _timeLabel.textAlignment = 2;
    _contectLabel.text = chipModel.content;
    _likeLabel.text = [NSString stringWithFormat:@"评论:%@          喜欢:%@", [chipModel.counterList objectForKey:@"comment"], [chipModel.counterList objectForKey:@"like"]];
    _likeLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];
    _likeLabel.textAlignment = 2;
    _userNameLabel.text = [chipModel.userinfo objectForKey:@"uname"];
    _userNameLabel.font = [UIFont systemFontOfSize:15 * FitWidth];
    [_userNameLabel NightWithType:UIViewColorTypeNormal];
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userVc)];
    [_userImageView addGestureRecognizer:tap];
    _userImageView.userInteractionEnabled = YES;

    
}
-(void)userVc
{
    SHD_UserDataViewController *suv = [[SHD_UserDataViewController alloc]init];
    suv.ID = [_chipModel.userinfo objectForKey:@"uid"];
    [self.nav pushViewController:suv animated:YES];
    
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
